﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using LKVMLMADMIN.Models;
using LKVService;

namespace LKVMLMADMIN.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /News/

        public ActionResult ListNews(int page = 1)
        {
            if (Session["User"] != null)
            {
                var db = new LKVService.SystemService();
                int count = 0;
                var _getData = db.getListNewsPaging(null, (page - 1)*100, 100);
                ViewBag.listNews = _getData.Item2;
                count = _getData.Item1;
                int[] myArray = Enumerable.Repeat( - 1, count).ToArray();
                return View(myArray.ToPagedList(page,100));
            }
            else
                return RedirectToAction("Login", "Account");
        }
        [HttpPost]
        public ActionResult ListNews(string keyword = "")
        {
            var db = new LKVService.SystemService();
            int count = 0;
            int page = 1;
            var _getData = db.getListNewsPaging(keyword, (page - 1) * 100,100);
            ViewBag.listNews = _getData.Item2;
            count = _getData.Item1;
            int[] myArray = Enumerable.Repeat(-1, count).ToArray();
            return View(myArray.ToPagedList(page, 100));
        }
        public ActionResult NewsDetail(int id,string notice = "")
        {            
            if (Session["User"] != null)
            {
                if (id > 0) 
                { 
                    var db = new LKVService.SystemService();
                    var dmuc = db.getAllDanhMuc().ToList();
                    var _getData = db.finNewsById(id);
                    var model = new NewsModel()
                    {
                        DanhMuc_Id = _getData.DanhMuc_Id,
                        ID = _getData.ID,
                        NgayTao = _getData.NgayTao,
                        NguoiTao = _getData.NguoiTao,
                        NoiDung = _getData.NoiDung,
                        Status = _getData.Status,
                        TenBaiViet = _getData.TenBaiViet,
                        TenDanhMuc = _getData.TenDanhMuc,
                        DanhMucItem = dmuc.Select(s => new SelectListItem {
                            Value = s.ID.ToString(),
                            Text = s.Name,
                            Selected = _getData.DanhMuc_Id == s.ID ? true : false
                        }).AsEnumerable(),
                        _type = 1
                    };
                    ViewBag.Notice = notice;
                    return View(model);
                }else
                {
                    ViewBag.Notice = notice;
                    var db = new LKVService.SystemService();
                    var dmuc = db.getAllDanhMuc().ToList();
                    var model = new NewsModel() 
                    {
                        TenBaiViet = "",
                        NoiDung ="",
                        DanhMucItem = dmuc.Select(s => new SelectListItem
                        {
                            Value = s.ID.ToString(),
                            Text = s.Name
                        }).AsEnumerable(),
                        _type = 0
                    };
                    return View(model);
                }
            }
            else
                return RedirectToAction("Login", "Account");
        }
        [HttpPost]
        public ActionResult NewsDetail(NewsModel _nm)
        {
            var db = new LKVService.SystemService();
            if (ModelState.IsValid)
            {
                var tmV = new TinTucView() { 
                   ID = _nm.ID,
                   DanhMuc_Id = _nm.DanhMuc_Id,
                   TenBaiViet = _nm.TenBaiViet,
                   NoiDung = _nm.NoiDung,
                   Status = _nm.Status,
                };
                bool kq = db.EditNews(tmV);
                if (kq == true)
                    return RedirectToAction("ListNews", "News");
                else
                    return RedirectToAction("NewsDetail", "News", new { id = _nm.ID, notice = "Cập nhật thất bại" });
            }
            return RedirectToAction("NewsDetail", "News", new { id = _nm.ID, notice = "" });
        }
        [HttpPost]
        public ActionResult AddNews(NewsModel _nm)
        {
            var user = Session["User"]  as LKVMLMLib.UserProfile;
            var db = new LKVService.SystemService();
            var newsAdd = new TinTucView()
            {
                DanhMuc_Id = _nm.DanhMuc_Id,
                NguoiTao = user.UserName,
                NoiDung = _nm.NoiDung,
                TenBaiViet = _nm.TenBaiViet
            };
            bool kq = db.AddNews(newsAdd);
            if(kq == true)
            {
                return RedirectToAction("ListNews", "News");
            }else
                return RedirectToAction("NewsDetail", "News", new { id = 0, notice = "Thêm thất bại" });
        }
        public ActionResult DeleteNews(int Id)
        {
            var db = new LKVService.SystemService();
            bool kq = db.DeleteNews(Id);
            if (kq == true) {
                return RedirectToAction("ListNews", "News");
            }else
                return RedirectToAction("ListNews", "News", new { id = 0, notice = "Xóa thất bại" });
        }
        public ActionResult MainCategory(int Id = 0,string err ="")
        {
            var db = new LKVService.SystemService();
            var model = new LKVMLMLib.DanhMucTinTuc();
            if (Id != 0)
                model = db.findDanhMucTinTucById(Id);
            var _cate = db.getAllDanhMuc();
            ViewBag.DanhMuc = _cate;
            ViewBag.Error = err;
            return View(model); 
        }
        public ActionResult AddCategory(LKVMLMLib.DanhMucTinTuc dm)
        {
            var db = new LKVService.SystemService();
            if (checkControls(dm) == null) 
            { 
                bool _kq = db.AddCategoryNews(dm);
                if (_kq == true)
                    return RedirectToAction("MainCategory", "News", new { Id = 0 }); 
                else
                    return RedirectToAction("MainCategory", "News", new { Id = 0,err = "Thêm thất bại" });
            }else
                return RedirectToAction("MainCategory", "News", new { Id = 0, err = checkControls(dm) });
        }
        public ActionResult EditCategory(LKVMLMLib.DanhMucTinTuc dm)
        {
            var db = new LKVService.SystemService();
            if (checkControls(dm) == null)
            {
                bool _kq = db.EditCategoryNews(dm);
                if (_kq == true)
                    return RedirectToAction("MainCategory", "News", new { Id = 0 });
                else
                    return RedirectToAction("MainCategory", "News", new { Id = 0, err = "Cập nhật thất bại" });
            }
            else
                return RedirectToAction("MainCategory", "News", new { Id = 0, err = checkControls(dm) });
        }
        public ActionResult DeleteCategory(int Id)
        {
            var db = new LKVService.SystemService();
            bool _kq = db.DeletCategoryNews(Id);
            if (_kq == true)
                return RedirectToAction("MainCategory", "News", new { Id = 0 });
            else
                return RedirectToAction("MainCategory", "News", new { Id = 0, err = "Xóa thất bại" });
        }
        public string checkControls(LKVMLMLib.DanhMucTinTuc dm)
        {
            if (String.IsNullOrEmpty(dm.Name))
                return "Nhập tên danh mục";
            return null;
        }
    }
}
